package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Solution4 {

    /**
     *
     * [14:21] Jiahui Sun
     *  validates if the number range overlap exists in a list of number tuples
     * // Sample Input: [(1,3), (2,5), (6,8)]
     * // Return: true
     */

    public static void main(String[] args) {
        int[][] arr = {{1, 3}, {2, 5}, {6, 8}};
        System.out.println(new Solution4().overlap(arr));
    }
    public Boolean overlap(int[][] arr){

        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[1];
            }
        });

        System.out.println(arr);

        for (int i = 1; i < arr.length; i++) {
            int[] cur = arr[i];
            int[] pre = arr[i - 1];
            int curFirst = cur[0];
            int preLast = pre[1];
            if (curFirst <= preLast) {
                return true;
            }
        }
        return false;
    }


    public Boolean overlap1(int[][] arr){

        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[1];
            }
        });

        List<int[]> list = new ArrayList<>();
        System.out.println(arr);

        int[] pre = arr[0];

        for (int i = 1; i < arr.length; i++) {
            int[] cur = arr[i];
            int curFirst = cur[0];
            int curLast = cur[1];
            int preLast = pre[1];
            int[] newArr = new int[2];
            if (curFirst <= preLast) {
                pre[0] = curFirst;
                if (curLast < preLast) {
                    pre[1] = curLast;
                    list.add(pre);
                } else {
                    pre[1] = preLast;

                }
            } else {
                list.add(pre);
                pre = cur;
            }
        }
        return false;
    }
}
